package view;

import java.awt.event.KeyEvent;
import model.Administrador;
import model.Candidato;
import model.Cargo;
import util.UrnaUtil;
import util.ViewUtil;

public class SelecaoPerfil extends javax.swing.JFrame {

  public SelecaoPerfil() {
    initComponents();
    setTitle("Urna Eletrônica");
    setLocationRelativeTo(null);
    setResizable(false);
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanelCadastraSenha = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jPasswordField1 = new javax.swing.JPasswordField();
    jLabel2 = new javax.swing.JLabel();
    jPasswordField2 = new javax.swing.JPasswordField();
    jLabelConfirmacaoSenha = new javax.swing.JLabel();
    jButtonGravarSenha = new javax.swing.JButton();
    jPanelConfirmaSenha = new javax.swing.JPanel();
    jLabel3 = new javax.swing.JLabel();
    jPasswordField3 = new javax.swing.JPasswordField();
    jButtonConfirmarSenha = new javax.swing.JButton();
    jLabelSenhaIncorreta = new javax.swing.JLabel();
    jPanelCentro = new javax.swing.JPanel();
    jLabelSelecaoPerfil = new javax.swing.JLabel();
    btnEleitor = new javax.swing.JButton();
    btnAdministrador = new javax.swing.JButton();
    jLabel4 = new javax.swing.JLabel();

    jPanelCadastraSenha.setBackground(new java.awt.Color(0, 51, 51));
    jPanelCadastraSenha.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(255, 255, 255));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Informe a senha:");
    jPanelCadastraSenha.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 528, 33));

    jPasswordField1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
    jPasswordField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jPasswordField1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
    jPanelCadastraSenha.add(jPasswordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 33, 260, 33));

    jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
    jLabel2.setForeground(new java.awt.Color(255, 255, 255));
    jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel2.setText("Confirme a senha:");
    jPanelCadastraSenha.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 66, 528, 33));

    jPasswordField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jPasswordField2.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
    jPasswordField2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jPasswordField2ActionPerformed(evt);
      }
    });
    jPanelCadastraSenha.add(jPasswordField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 100, 260, 33));

    jLabelConfirmacaoSenha.setFont(new java.awt.Font("Segoe UI", 2, 14)); // NOI18N
    jLabelConfirmacaoSenha.setForeground(new java.awt.Color(204, 255, 255));
    jLabelConfirmacaoSenha.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabelConfirmacaoSenha.setText(" ");
    jPanelCadastraSenha.add(jLabelConfirmacaoSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 200, 528, 30));

    jButtonGravarSenha.setBackground(new java.awt.Color(0, 153, 204));
    jButtonGravarSenha.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
    jButtonGravarSenha.setForeground(new java.awt.Color(255, 255, 255));
    jButtonGravarSenha.setText("Gravar");
    jButtonGravarSenha.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255), 2));
    jButtonGravarSenha.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonGravarSenhaActionPerformed(evt);
      }
    });
    jPanelCadastraSenha.add(jButtonGravarSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 150, 260, 40));

    jPanelConfirmaSenha.setBackground(new java.awt.Color(0, 51, 51));
    jPanelConfirmaSenha.setMinimumSize(new java.awt.Dimension(450, 200));
    jPanelConfirmaSenha.setPreferredSize(new java.awt.Dimension(450, 200));
    jPanelConfirmaSenha.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jLabel3.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
    jLabel3.setForeground(new java.awt.Color(255, 255, 255));
    jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel3.setText("Informe a senha:");
    jPanelConfirmaSenha.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 450, 50));

    jPasswordField3.setFont(new java.awt.Font("Segoe UI", 0, 36)); // NOI18N
    jPasswordField3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jPasswordField3.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
    jPasswordField3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jPasswordField3ActionPerformed(evt);
      }
    });
    jPasswordField3.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jPasswordField3KeyPressed(evt);
      }
    });
    jPanelConfirmaSenha.add(jPasswordField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, 240, 40));

    jButtonConfirmarSenha.setBackground(new java.awt.Color(0, 153, 204));
    jButtonConfirmarSenha.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
    jButtonConfirmarSenha.setForeground(new java.awt.Color(255, 255, 255));
    jButtonConfirmarSenha.setText("Confirmar");
    jButtonConfirmarSenha.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
    jButtonConfirmarSenha.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonConfirmarSenhaActionPerformed(evt);
      }
    });
    jPanelConfirmaSenha.add(jButtonConfirmarSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 110, 240, 40));

    jLabelSenhaIncorreta.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
    jLabelSenhaIncorreta.setForeground(new java.awt.Color(204, 255, 255));
    jLabelSenhaIncorreta.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jPanelConfirmaSenha.add(jLabelSenhaIncorreta, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 160, 290, 30));

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanelCentro.setBackground(new java.awt.Color(0, 51, 51));

    jLabelSelecaoPerfil.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
    jLabelSelecaoPerfil.setForeground(new java.awt.Color(255, 255, 255));

    btnEleitor.setBackground(new java.awt.Color(0, 153, 204));
    btnEleitor.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
    btnEleitor.setForeground(new java.awt.Color(255, 255, 255));
    btnEleitor.setText("Eleitor");
    btnEleitor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
    btnEleitor.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnEleitorActionPerformed(evt);
      }
    });

    btnAdministrador.setBackground(new java.awt.Color(0, 153, 204));
    btnAdministrador.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
    btnAdministrador.setForeground(new java.awt.Color(255, 255, 255));
    btnAdministrador.setText("Administrador");
    btnAdministrador.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
    btnAdministrador.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnAdministradorActionPerformed(evt);
      }
    });

    jLabel4.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
    jLabel4.setForeground(new java.awt.Color(255, 255, 255));
    jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel4.setText("Selecione o Perfil");

    javax.swing.GroupLayout jPanelCentroLayout = new javax.swing.GroupLayout(jPanelCentro);
    jPanelCentro.setLayout(jPanelCentroLayout);
    jPanelCentroLayout.setHorizontalGroup(
      jPanelCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanelCentroLayout.createSequentialGroup()
        .addGap(24, 24, 24)
        .addGroup(jPanelCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jLabelSelecaoPerfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelCentroLayout.createSequentialGroup()
            .addComponent(btnEleitor, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addComponent(btnAdministrador, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addGap(24, 24, 24))
    );
    jPanelCentroLayout.setVerticalGroup(
      jPanelCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanelCentroLayout.createSequentialGroup()
        .addGap(25, 25, 25)
        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanelCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(btnAdministrador, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(btnEleitor, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jLabelSelecaoPerfil, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
        .addContainerGap())
    );

    getContentPane().add(jPanelCentro, java.awt.BorderLayout.CENTER);

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void jButtonGravarSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGravarSenhaActionPerformed
      String senha = new String(jPasswordField1.getPassword());
      String ConfirmSenha = new String(jPasswordField2.getPassword());

      if (ViewUtil.isEmpty(jPanelCadastraSenha.getComponents())) {
        if (senha.equals(ConfirmSenha)) {

          Administrador.setSenhaArquivo(senha);
          Principal.main("Administrador");
          this.dispose();
        } else {

          jLabelConfirmacaoSenha.setText("Senhas não conferem !");
          jPasswordField1.requestFocus();
        }
      } else {

        jLabelConfirmacaoSenha.setText("Preencha todos os campos !");
      }
    }//GEN-LAST:event_jButtonGravarSenhaActionPerformed

    private void jButtonConfirmarSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmarSenhaActionPerformed

      String senha = new String(jPasswordField3.getPassword());

      if (senha.equals(Administrador.getSenhaArquivo())) {

        Principal.main("Administrador");
        this.dispose();

      } else {
        jLabelSenhaIncorreta.setText("Senha Incorreta !");
      }
    }//GEN-LAST:event_jButtonConfirmarSenhaActionPerformed

    private void btnEleitorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEleitorActionPerformed
      
      if(Candidato.isEmpty() || Cargo.isEmpty()) {
       jLabelSelecaoPerfil.setText("<html>Não existem Cargos ou Candidatos previamente cadastrados.<br>"
               + "Por favor cadastre-os no perfil Administrador !</html>");
      } else {
        Principal.main("Eleitor");
        this.dispose();
      }
    }//GEN-LAST:event_btnEleitorActionPerformed

    private void btnAdministradorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdministradorActionPerformed
      if (Administrador.arquivoExist()) {
        ViewUtil.trocaPanels(jPanelCentro, jPanelConfirmaSenha);
        jPasswordField3.requestFocus();
      } else {
        ViewUtil.trocaPanels(jPanelCentro, jPanelCadastraSenha);
      }
    }//GEN-LAST:event_btnAdministradorActionPerformed

    private void jPasswordField3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordField3KeyPressed

      if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        jButtonConfirmarSenhaActionPerformed(null);
      } else {
        jLabelSenhaIncorreta.setText("");
      }

    }//GEN-LAST:event_jPasswordField3KeyPressed

    private void jPasswordField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField3ActionPerformed
      // TODO add your handling code here:
    }//GEN-LAST:event_jPasswordField3ActionPerformed

    private void jPasswordField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField2ActionPerformed
      // TODO add your handling code here:
    }//GEN-LAST:event_jPasswordField2ActionPerformed

  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new SelecaoPerfil().setVisible(true);
        UrnaUtil.verificaPastasExist();
      }
    });
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btnAdministrador;
  private javax.swing.JButton btnEleitor;
  private javax.swing.JButton jButtonConfirmarSenha;
  private javax.swing.JButton jButtonGravarSenha;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabelConfirmacaoSenha;
  private javax.swing.JLabel jLabelSelecaoPerfil;
  private javax.swing.JLabel jLabelSenhaIncorreta;
  private javax.swing.JPanel jPanelCadastraSenha;
  private javax.swing.JPanel jPanelCentro;
  private javax.swing.JPanel jPanelConfirmaSenha;
  private javax.swing.JPasswordField jPasswordField1;
  private javax.swing.JPasswordField jPasswordField2;
  private javax.swing.JPasswordField jPasswordField3;
  // End of variables declaration//GEN-END:variables

}
